<?php
/**
 * Created by PhpStorm.
 * User: Syousoft
 * Date: 17/2/17
 * Time: 上午10:12
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class PhotoController extends AdminbaseController{

    protected $photo_model;

    function _initialize() {
        parent::_initialize();
        $this->photo_model = D("Admin/PhotoWall");
    }

    //照片墙列表
    public function index(){

        //所有比赛
        $matches=M('match')->select();
        $this->assign('matches',$matches);

        $where=array();
        $match_id=I('match_id');
        if(!empty($match_id)){
            $where['pm.match_id']=$match_id;
        }else{
            //默认第一个比赛的照片
            $match_id=$matches[0]['id'];
            if(!empty($match_id)){
                $where['pm.match_id']=$match_id;
            }
        }
        $count=$this->photo_model->alias('pm')->where($where)->join('left join hs_match as m on pm.match_id=m.id')->count();
        $page=$this->page($count,20);

        $posts=$this->photo_model->
            alias('pm')->where($where)->join('left join hs_match as m on pm.match_id=m.id')->
            limit($page->firstRow,$page->listRows)->order("pm.create_at DESC")->field("pm.id as pid,pm.*,m.*")->select();
        $this->assign("page",$page->show());
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);

        $this->display();
    }

    //添加照片墙
    public function add(){
        $matches=M('match')->select();
        $this->assign('matches',$matches);
        $this->display();
    }

    public function add_post(){
        $data['create_at']=date("Y-m-d h:i:s",time());
        if(IS_POST){
            if ($this->photo_model->create()!==false) {
                if ($this->photo_model->add($_POST,$data)!==false) {
                    $this->success("添加成功！", U("Photo/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->photo_model->getError());
            }
        }
    }

    public function edit(){

    }

    public function delete(){
        $id=I('id');
        $result=$this->photo_model->delete($id);
        if($result!==false){
            exit(json_encode(array("status"=>true,"message"=>"删除成功")));
        }else{
            exit(json_encode(array("status"=>false,"message"=>"删除失败")));
        }

    }

}